package com.op.teachingliteapp.fragment;

import java.io.File;

import org.vudroid.core.DecodeService;
import org.vudroid.core.DecodeServiceBase;
import org.vudroid.core.DocumentView;
import org.vudroid.core.ViewerPreferences;
import org.vudroid.core.events.CurrentPageListener;
import org.vudroid.core.events.DecodingProgressListener;
import org.vudroid.core.models.CurrentPageModel;
import org.vudroid.core.models.DecodingProgressModel;
import org.vudroid.core.models.ZoomModel;
import org.vudroid.core.views.PageViewZoomControls;
import org.vudroid.pdfdroid.codec.PdfContext;

import android.app.Activity;
import android.app.Fragment;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;

/**
 * Created by Genji on 2015/6/23.
 * 无效类
 */
public class PowerPointFragment extends Fragment implements DecodingProgressListener, CurrentPageListener {
    private static final String TAG = PowerPointFragment.class.getSimpleName();
	private Activity activity;
    private DecodeService decodeService;
    private DocumentView documentView;
    private ViewerPreferences viewerPreferences;
    private CurrentPageModel currentPageModel;
    private static final String DOCUMENT_VIEW_STATE_PREFERENCES = "DjvuDocumentViewState";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initDecodeService();
        activity = getActivity();


        final ZoomModel zoomModel = new ZoomModel();
        final DecodingProgressModel progressModel = new DecodingProgressModel();
        progressModel.addEventListener(this);
        currentPageModel = new CurrentPageModel();
        currentPageModel.addEventListener(this);
        documentView = new DocumentView(activity, zoomModel, progressModel, currentPageModel);
        zoomModel.addEventListener(documentView);
        documentView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));

        decodeService.setContentResolver(activity.getContentResolver());
        decodeService.setContainerView(documentView);
        documentView.setDecodeService(decodeService);
        Uri uri =Uri.fromFile(new File(getArguments().getString("url")));

        decodeService.open(uri);
        viewerPreferences = new ViewerPreferences(activity);

        final FrameLayout frameLayout = createMainContainer();
        frameLayout.addView(documentView);
//        frameLayout.addView(createZoomControls(zoomModel));
//        setFullScreen();


        final SharedPreferences sharedPreferences = activity.getSharedPreferences(DOCUMENT_VIEW_STATE_PREFERENCES, 0);
        documentView.goToPage(sharedPreferences.getInt(uri.toString(), 0));
        documentView.showDocument();
        viewerPreferences.addRecent(uri);
        return frameLayout;
    }

    @Override
    public void decodingProgressChanged(int currentlyDecoding) {
        Log.i(TAG,"--------------decodingProgressChanged------------");


    }

    @Override
    public void currentPageChanged(int pageIndex) {
        Log.i(TAG,"--------------currentPageChanged------------");


    }

    private FrameLayout createMainContainer() {
        return new FrameLayout(activity);
    }

    private PageViewZoomControls createZoomControls(ZoomModel zoomModel) {
        final PageViewZoomControls controls = new PageViewZoomControls(activity, zoomModel);
        controls.setGravity(Gravity.RIGHT | Gravity.BOTTOM);
        zoomModel.addEventListener(controls);
        return controls;
    }

    private void setFullScreen() {
        if (viewerPreferences.isFullScreen()) {
            activity.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
            activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        } else {
            activity.getWindow().requestFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        }
    }

    private DecodeService createDecodeService() {
        return new DecodeServiceBase(new PdfContext());


    }
    private void initDecodeService()
    {
        if (decodeService == null)
        {
            decodeService = createDecodeService();
        }
    }

}